package com.usian.springbootmysql.service.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usian.springbootmysql.dao.LoginDao;
import com.usian.springbootmysql.dao.UserDao;
import com.usian.springbootmysql.pojo.ResultVo;
import com.usian.springbootmysql.pojo.TbUser;
import com.usian.springbootmysql.service.LoginService;
import com.usian.springbootmysql.utils.AliCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    LoginDao loginDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private UserDao userDao;

    /**
     * 发送登录验证码
     * @param phone 登录的手机号
     * @return
     */
    @Override
    public ResultVo sendCode(String phone) throws Exception {
        //1.判断是否是一个正确的手机号
       Boolean isPhone = Validator.isMobile(phone);
       if (isPhone){
           //2.判断手机号是否已经注册
           int count = loginDao.getUserCountByPhone(phone);
           if (count>0){
               //手机号已经注册
               //3.发送验证码
               AliCodeUtils aliCodeUtils = new AliCodeUtils();
               aliCodeUtils.sendLoginCode(phone,"123456");
               stringRedisTemplate.opsForValue().set(phone,"123456");
               stringRedisTemplate.expire(phone,1, TimeUnit.MINUTES);
               return new ResultVo(200,"短信发送成功",null);
           }else {
               return new ResultVo(303,"此手机号不存在或者尚未激活",null);
           }

       }else {
           return new ResultVo(303,"手机号码格式错误",null);
       }
    }

    @Override
    public ResultVo userLogin(String phone, String code) {
//        1、判断手机号和验证码是否填写
        boolean isHavephone = StrUtil.isEmpty(phone);
        boolean isHaveCode = StrUtil.isEmpty(code);
        if (!isHavephone){
            if (!isHaveCode){
        //        2、用户判断是否已经存在
                LambdaQueryWrapper<TbUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(TbUser::getPhone,phone);
                TbUser tbUser = userDao.selectOne(lambdaQueryWrapper);
//                int count = loginDao.getUserCountByPhone(phone);
                if (tbUser!=null){
                    //        3、验证验证码是否正确,匹配用户输入的验证码，和我们保存的验证码是否一致
                    String redisCode = stringRedisTemplate.opsForValue().get(phone);
                    if (!StrUtil.isEmpty(redisCode)){
                        if (redisCode.equals(code)){
                            //         4、返回用户个人信息
                            return new ResultVo(200,"success",tbUser);
                        }else {
                            return new ResultVo(303,"验证码输入错误",null);
                        }
                    }else {
                        return new ResultVo(303,"验证码已超期",null);
                    }


                }else {
                    return new ResultVo(303,"此手机号不存在或者尚未激活哦",null);
                }
            }else {
                return new ResultVo(303,"验证码必填",null);
            }
        }else {
            return new ResultVo(303,"手机号码必填",null);
        }
    }
}
